 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : EasyAvatar-Android
 * Package  : net.solosky.easyavatar.view
 * File     : ContactListAdapter.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : Nov 20, 2011
 * License  : Apache License 2.0 
 */
package net.solosky.easyavatar.view;

import java.util.List;

import net.solosky.easyavatar.R;
import net.solosky.easyavatar.bean.Contact;
import net.solosky.easyavatar.utils.Utils;
import android.content.Context;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 *
 * 选择联系人适配器
 *
 * @author solosky <solosky772@qq.com>
 *
 */
public class ContactListAdapter extends SimpleListAdapter<Contact>  {
	private int selectedPosition;
	public ContactListAdapter(Context context, List<Contact> dataList) {
		super(context, dataList);
		selectedPosition = -1;
	}

	@Override
	public ViewHolder newView(int position) {
		View view = inflate(R.layout.li_contact_item);
		ContactHolder holder = new ContactHolder();
		holder.contentView = view;
		holder.ivAvatar = (ImageView) view.findViewById(R.id.iv_contact_avatar);
		holder.tvName   = (TextView)  view.findViewById(R.id.tv_contact_name);
		holder.tvMobile = (TextView)  view.findViewById(R.id.tv_contact_mobile);
		holder.ivSelected = (ImageView) view.findViewById(R.id.iv_contact_selected);
		return holder;
	}

	@Override
	public void fillView(ViewHolder viewHolder, int position) {
		Contact contact = (Contact) getItem(position);
		ContactHolder holder = (ContactHolder) viewHolder;
		if(contact.getHead()!=null){
			holder.ivAvatar.setImageBitmap(contact.getHead());
		}else{
			holder.ivAvatar.setImageResource(R.drawable.default_avatar);
		}
		holder.tvMobile.setText(contact.getMobile());
		holder.tvName.setText(contact.getName());
		holder.ivSelected.setVisibility(selectedPosition==position?View.VISIBLE:View.INVISIBLE);
		holder.contentView.setBackgroundColor(getContext().getResources().getColor(selectedPosition==position?R.color.lightgreen:R.color.white));
	}
	
	public int getSelectedPosition(){
		return selectedPosition;
	}
	
	public void setSeletedPoistion(int position){
		selectedPosition = position;
	}
	
	public class ContactHolder extends ViewHolder{
		public ImageView ivAvatar;
		public TextView  tvName;
		public TextView  tvMobile;
		public ImageView ivSelected;
	}
}
